﻿Imports DTO
Imports BUS
Imports DAO
Imports System.Data.Linq

Public Class ucQuyDinh

    Public Sub Load_QuyDinh()
        Dim qds As IQueryable(Of ThongSo)
        Dim tsBUS As New ThongSoBUS
        qds = tsBUS.LayBang()

        dgvQuyDinh.DataSource = qds
        dgvQuyDinh.Columns(0).HeaderText = "Quy Định"
        dgvQuyDinh.Columns(0).ReadOnly = True
        dgvQuyDinh.Columns(1).HeaderText = "Giá trị"
        dgvQuyDinh.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    End Sub

    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        Dim regex = "^[0-9]+$"
        Dim i = 0
        lbThongBao.Text = ""
        For Each dgvRow As DataGridViewRow In dgvQuyDinh.Rows
            lbThongBao.Visible = True
            i = i + 1
            Dim tsBUS As New ThongSoBUS
            Dim tsDTO As New ThongSoDTO

            tsDTO.TenQD = dgvRow.Cells("TenQD").Value.ToString()
            Integer.TryParse(dgvRow.Cells("GiaTri").Value.ToString(), tsDTO.GiaTri)

            If Not Check(tsDTO.GiaTri.ToString(), regex) Then
                lbThongBao.Text &= "Dòng " + i.ToString() + "  "
                Continue For
            End If

            If tsDTO.TenQD = "QD4" AndAlso Not Check(tsDTO.GiaTri, "^[01]{1}$") Then
                lbThongBao.Text &= "Dòng " + i.ToString() + "  "
                Continue For
            End If

            tsBUS.CapNhat(tsDTO)
        Next
        If lbThongBao.Text <> "" Then
            lbThongBao.Text += "không đúng quy định."
        Else
            lbThongBao.Text = "Cập nhật thành công."
        End If
    End Sub

    Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
        Dim qds As IQueryable(Of ThongSo)
        Dim tsBUS As New ThongSoBUS
        qds = tsBUS.LayBang()

        dgvQuyDinh.DataSource = qds
        dgvQuyDinh.Columns(0).HeaderText = "Quy Định"
        dgvQuyDinh.Columns(0).ReadOnly = True
        dgvQuyDinh.Columns(1).HeaderText = "Giá trị"
        dgvQuyDinh.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
    End Sub
End Class
